home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpsehttp / cgi-bin / aglimpse next >
Text File  |  1995-05-16  |  8KB  |  309 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Acknowledgements
  4. #
  5. # Thanks to Guy Brooker (guy@jw.estec.esa.nl) for his AA interface,
  6. # which was the starting point for this program.
  7. #
  8. # Paul Clark
  9. # paul@cs.arizona.edu
  10. #
  11. # Modifications
  12. #
  13. # 2/22/94    Version 1.0, shell script version    Paul Clark
  14. # 4/21/94    Version 1.1, multiple archives support    Paul Clark
  15. # 4/22/94    Version 1.2, perl script        Paul Clark
  16. # 8/05/94    Version 1.3, verbosity&security        Paul Clark
  17. #10/05/94    Version 1.4, more security, improved
  18. #                output            Paul Clark
  19.  
  20. # **** **** **** ****    CONFIGURABLE VARIABLES     **** **** **** ****
  21. $HTTPD_HOME="/usr1/paul/httpd" ;
  22. $HTTPD_NEWSHOME="/usr1/paul/news" ;
  23. $GLIMPSE_LOC="/usr/paul/bin/glimpse" ;
  24.  
  25. $CONVERT="$HTTPD_HOME/wwwlib/cvtwww" ;
  26. $FSSERV="/cgi-bin/mfs" ;
  27.  
  28. # Set file name pattern where to suppress HTML tags
  29. # Comment out to cancel suppression
  30. # Currently set to "only filenames ending with '.html'"
  31. $SUPPRESS_HTML_TAGS = "\\.html\$";
  32.  
  33. # **** **** **** **** NO CONFIGURATION NEEDED BELOW **** **** **** ****
  34.  
  35. $_ = $ENV{'PATH_INFO'};
  36. if ( m|^/([0-9][0-9]*)(.*)$| ) {
  37.     $script = $1;
  38.     $path = $2;
  39.     $path =~ s|"||g;
  40. } else {
  41.     &err_noscript;
  42. }
  43.  
  44. open(AMGRCONF,"$HTTPD_HOME/wwwlib/amgr.cfg") || &err_conf;
  45. undef $indexdir;
  46. line: while (<AMGRCONF>) {
  47.     @_ = split(/\t/);
  48.     if ( $_[3] eq $script ) {
  49.         $indexdir = $_[0];
  50.         $urlpath = $_[1];
  51.         last line;
  52.     }
  53. }
  54. &err_noscript unless $indexdir;
  55. close(AMGRCONF);
  56. ($ENV{'HOME'} = $indexdir) || &err_noscript; # some versions of Glimpse need it
  57.  
  58. # Ensure that Glimpse is available on this machine
  59. -x $GLIMPSE_LOC || &err_noglimpse ;
  60.  
  61. # Ensure that index is available
  62. -r "$indexdir/.glimpse_index" || &err_noindex($indexdir) ;
  63.  
  64. #    To support an ISINDEX type search, set query string if given
  65. #    an argument on the command line
  66. $prefix="whole=on&case=on&query=" if ( $#ARGV >= 0 );
  67.  
  68. #    Check that a query has been made
  69. ($query = $ENV{'QUERY_STRING'}) || &err_noquery ;
  70.  
  71. #    Strip the variables out from the query string,
  72. #    and assign them into variables, prefixed by 'QS_'
  73. @qvars = split( /\&/, $prefix . $query );
  74. foreach (@qvars) {
  75.     split(/=/);
  76.     $fname = $_[0];
  77.     $fvalue = $_[1];
  78.     $fvalue =~ s/\'//g;
  79.     $cmd = "\$QS_$fname = '$fvalue';" ;
  80.     # print ">>>",$cmd,"\n";
  81.     $cmd = eval $cmd if ( $fname =~ /^[a-z_A-Z]\w*$/ );
  82. }
  83. $QS_query =~ s|\+| |g;
  84. $QS_query =~ s|%(\w\w)|sprintf("%c", hex($1))|ge;
  85. $pquery = $QS_query;
  86. $QS_query =~ s|\'|\'\"\'\"\'|g;
  87.  
  88. $OPT_errors="-$QS_errors"    if $QS_errors =~ /^[0-8]$/;
  89. $OPT_errors="-B"        if $QS_errors =~ /^Best\+match$/;
  90. $OPT_case="-i"            if $QS_case =~ /^on$/;
  91. $OPT_whole="-w"            unless $QS_whole =~ /^on$/;
  92. $path =~ s/\./\\./;
  93. $path =~ s/\'//g;
  94. $OPT_filter="-F '$path'"    if $path;
  95.  
  96. if ($QS_maxlines =~ /\d+/) {
  97.     $maxlines = $&;
  98. } else {
  99.     $maxlines = 20;
  100. }
  101. if ($QS_maxfiles =~ /\d+/) {
  102.     $maxfiles = $&;
  103. } else {
  104.     $maxfiles = 100;
  105. }
  106.  
  107. $highlight = $QS_query;
  108. $highlight =~ s/^\W+//;
  109. $highlight = join("|",split(/\W+/,$highlight));
  110. # check if the query contains any words
  111. &err_badquery if !$highlight;
  112. $highlight = '\b('.$highlight.')\b' if $OPT_whole;
  113.  
  114. print "Content-type: text/html\n\n" ;
  115. print "<HEAD><TITLE>Result for query \"$pquery\"\n";
  116. print "</TITLE></HEAD><BODY>\n";
  117. print "<H1>Result for query \"$pquery\"</H1><HR>\n";
  118.  
  119. chdir $indexdir;
  120. $cmd = "exec $GLIMPSE_LOC -y -n $OPT_case $OPT_whole $OPT_errors -H . " .
  121.      "$OPT_filter '$QS_query' 2>&1 |";
  122. $gpid = open(GOUT, $cmd );
  123. $prevfile = "";
  124. $lcount = 0;
  125. $fcount = 0;
  126. line: while (<GOUT>) {
  127.     ( /^([^:]*):\s*(\d+):(.*)/ ) || next;
  128.     $file = $1;
  129.     $line = $2;
  130.     $string = $3;
  131.     next unless $file =~ s|^$indexdir||o;
  132.     if ($file ne $prevfile) {
  133.         $linecount = 0;
  134.         if ($fcount>$maxfiles) {
  135.             print "<H3>Limit of $maxfiles files exceeded...</H3>\n";
  136.             $file = "";
  137.             $fcount = "at least $fcount";
  138.             $lcount = "at least $lcount";
  139.             last line;
  140.         }
  141.         print "</UL>" if ( $prevfile ne "" );
  142.         $prevfile = $file ;
  143.         print "<H3>File <A HREF=\"",$FSSERV,"/",$script,$file,
  144.             "\">/",$urlpath,$file,"</A></H3><UL>\n" ;
  145.         $fcount++ ;
  146.     }
  147.     $lcount++ ;
  148.     $linecount++;
  149.     if ($linecount>=$maxlines) {
  150.         print "<LI>Limit of $maxlines matched " .
  151.             "lines per file exceeded...\n" if
  152.                 $linecount==$maxlines;
  153.         next line;
  154.     }
  155.     if ($SUPPRESS_HTML_TAGS && $file =~ /$SUPPRESS_HTML_TAGS/o) {
  156.         $string =~ s#\</?[a-zA-Z][^>\n]*\>?##g;
  157.     }
  158.     $string =~ s/\&/\&/g;
  159.     $string =~ s/\</\</g;
  160.     $string =~ s/\>/\>/g;
  161.     if ($OPT_case) {
  162.         $string =~ s#$highlight#<B>$&</B>#gio;
  163.     } else {
  164.         $string =~ s#$highlight#<B>$&</B>#go;
  165.     }
  166.     print "<LI><A HREF=\"",$FSSERV,"/",$script,$file,"?",$line,"#mfs\">\n" ;
  167.     print "line ",$line,":",$string,"</A>\n" ;
  168. }
  169. print "</UL>\n" if $file ;
  170. print "<HR>" ;
  171. print "<H2>Summary for query <code>\"",$QS_query,"\":</code></H2>\n" ;
  172. print "found ",$lcount," matches in ",$fcount," files\n" ;
  173. print "</BODY>\n" ;
  174. close(GOUT);
  175. unlink "/tmp/.glimpse_tmp.$gpid";
  176.  
  177. sub diag_exit {
  178. # exit on error
  179.     exit 1;
  180. }
  181. sub err_noquery {
  182. #    The script was called without a query. 
  183. #    Provide an ISINDEX type response for browsers
  184. #    without form support.
  185.     print <<'EOM' ;
  186. Content-type: text/html
  187.  
  188. <HEAD><TITLE>Glimpse Gateway</TITLE></HEAD>
  189. <BODY><H1>Glimpse Gateway</H1>
  190. This is a gateway to Glimpse.
  191. Type a pattern to search in your browser's search dialog.<P>
  192.  
  193. <ISINDEX>
  194.  
  195. <H2>What is Glimpse ?</H2>
  196. <QUOTE>
  197. <P>
  198. Glimpse (which stands  for  GLobal  IMPicit  SEarch)  is  an
  199. indexing  and query system that allows you to search through
  200. all your files very quickly.   For  example,  a  search  for
  201. Schwarzkopf  allowing  two  misspelling errors in 5600 files
  202. occupying 77MB took 7 seconds on a SUN  IPC.   Glimpse  supports
  203. most of agrep's options (agrep is our powerful version
  204. of  grep)  including  approximate  matching  (e.g.,  finding
  205. misspelled  words),  Boolean  queries, and even some limited
  206. forms of regular expressions.<BR>
  207. Glimpse's running time is typically slower than systems
  208. tems using inverted indexes, but its index is  an  order  of
  209. magnitude smaller (typically 2-5% of the size of the files).
  210. <H2>Authors of Glimpse</H2>
  211. Udi Manber, Sun Wu, and Burra Gopal<BR>
  212. <ADDRESS>
  213. Department of  Computer
  214. Science, University   of   Arizona,   Tucson,   AZ  85721.<BR>
  215. glimpse@cs.arizona.edu
  216. </ADDRESS>
  217. </QUOTE>
  218.  
  219. <HR>
  220. <ADDRESS>
  221. Paul Clark<BR>
  222. paul@cs.arizona.edu<BR>
  223. </ADDRESS>
  224.  
  225. </BODY>
  226. EOM
  227.     &diag_exit;
  228. }
  229.  
  230. sub err_noglimpse {
  231. #
  232. # Glimpse was not found
  233. # Report a useful message
  234. #
  235.     print <<'EOM' ;
  236. Content-type: text/html
  237.  
  238. <HEAD>
  239. <TITLE>Glimpse not found</TITLE>
  240. </HEAD>
  241. <BODY>
  242. <H1>Glimpse not found</H1>
  243.  
  244. This gateway relies on <CODE>Glimpse</CODE> search tool.
  245. If it is installed, please set the correct path in the script file.
  246. Otherwise obtain the latest version from
  247. <A HREF="file://ftp.cs.arizona.edu/glimpse">ftp.cs.arizona.edu</A>
  248. </BODY>
  249. EOM
  250.     &diag_exit;
  251. }
  252.  
  253. sub err_noindex {
  254.     local ($indexdir) = @_;
  255. # Glimpse index was not found
  256. # Give recommendations for indexing
  257.     print "Content-type: text/html\n\n";
  258.     print "<HEAD>\n";
  259.     print "<TITLE>Glimpse Index not found</TITLE>\n";
  260.     print "</HEAD>\n";
  261.     print "<BODY>\n";
  262.     print "<H1>Glimpse Index in directory '$indexdir' not found</H1>\n";
  263.     print "Glimpse cannot proceed without index.\n";
  264.     print "Please check if the directory being searched is indexed\n";
  265.     print "by <code>glimpseindex</code>.\n";
  266.     print "</BODY>\n";
  267.     &diag_exit;
  268. }
  269.  
  270. sub err_noscript {
  271. # Glimpse archive was not found
  272.     print "Content-type: text/html\n\n";
  273.     print "<HEAD>\n";
  274.     print "<TITLE>Glimpse Archive not found</TITLE>\n";
  275.     print "</HEAD>\n";
  276.     print "<BODY>\n";
  277.     print "<H1>Glimpse Archive not found</H1>\n";
  278.     print "Cannot find script \"$script\" in config file ".
  279.         "$HTTPD_HOME/wwwlib/amgr.cfg\n";
  280.     print "</BODY>\n";
  281.     &diag_exit;
  282. }
  283.  
  284. sub err_conf {
  285. # Glimpse archive Configuration File was not found
  286.     print "Content-type: text/html\n\n";
  287.     print "<HEAD>\n";
  288.     print "<TITLE>Glimpse Archive Configuration File not found</TITLE>\n";
  289.     print "</HEAD>\n";
  290.     print "<BODY>\n";
  291.     print "<H1>Glimpse Archive Configuration File not found</H1>\n";
  292.     print "Cannot open configuration file $HTTPD_HOME/wwwlib/amgr.cfg\n";
  293.     print "</BODY>\n";
  294.     &diag_exit;
  295. }
  296.  
  297. sub err_badquery {
  298.     print "Content-type: text/html\n\n";
  299.     print "<HEAD>\n";
  300.     print "<TITLE>Query is too broad</TITLE>\n";
  301.     print "</HEAD>\n";
  302.     print "<BODY>\n";
  303.     print "<H1>Query is too broad</H1>\n";
  304.     print "The query \"$pquery\" doesn't contain any words and ".
  305.         "thus will take too much time. Please refine your query.\n";
  306.     print "</BODY>\n";
  307.     &diag_exit;
  308. }
  309.